package com.jl.rocketmq.consumer.filter.tag;

import com.jl.rocketmq.consumer.RocketmqConstant;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //1.创建消费者Consumer，制定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(RocketmqConstant.consumerGroup1);
        //2.指定Nameserver地址
        consumer.setNamesrvAddr(RocketmqConstant.mq_namesrv_cluster);
        //3.订阅主题Topic和Tag
//        consumer.subscribe(RocketmqConstant.topic_filtertag, RocketmqConstant.tag_filtertag1);
//        consumer.subscribe(RocketmqConstant.topic_filtertag, RocketmqConstant.tag_filtertag2);
        consumer.subscribe(RocketmqConstant.topic_filtertag, RocketmqConstant.tag_filtertag1 + "|| " + RocketmqConstant.tag_filtertag2);

        //4.设置回调函数，处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {

            //接受消息内容
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                for (MessageExt msg : msgs) {
                    System.out.println("consumeThread=" + Thread.currentThread().getName() + "," + new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //5.启动消费者consumer
        consumer.start();
        System.out.println("消费者启动");
    }
}
